import { defineStore } from 'pinia';
import { ref } from 'vue';
import { Message } from '@arco-design/web-vue';
import cargoCategoryApi from '@/api/cargo-category';

/**
 * 货物分类状态管理
 */
export const useCargoCategoryStore = defineStore('cargo-category', () => {
  // 状态
  const loading = ref(false);
  const categories = ref([]);
  const categoryDetail = ref(null);

  /**
   * 获取货物分类列表
   * @param {Object} params - 查询参数
   * @returns {Promise} 请求结果
   */
  async function getCategoryList(params = {}) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.getCategoryList(params);
      
      if (response.code === 0) {
        categories.value = response.data;
        return {
          success: true,
          data: categories.value
        };
      } else {
        Message.error(response.message || '获取货物分类列表失败');
        return {
          success: false,
          message: response.message || '获取货物分类列表失败'
        };
      }
    } catch (error) {
      console.error('获取货物分类列表失败:', error);
      Message.error('获取货物分类列表失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  /**
   * 获取货物分类详情
   * @param {Number} id - 分类ID
   * @returns {Promise} 请求结果
   */
  async function getCategoryDetail(id) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.getCategoryDetail(id);
      
      if (response.code === 0) {
        categoryDetail.value = response.data;
        return {
          success: true,
          data: categoryDetail.value
        };
      } else {
        Message.error(response.message || '获取货物分类详情失败');
        return {
          success: false,
          message: response.message || '获取货物分类详情失败'
        };
      }
    } catch (error) {
      console.error('获取货物分类详情失败:', error);
      Message.error('获取货物分类详情失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  /**
   * 创建货物分类
   * @param {Object} data - 分类数据
   * @returns {Promise} 请求结果
   */
  async function createCategory(data) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.createCategory(data);
      
      if (response.code === 0) {
        Message.success('创建货物分类成功');
        return {
          success: true,
          data: response.data
        };
      } else {
        Message.error(response.message || '创建货物分类失败');
        return {
          success: false,
          message: response.message || '创建货物分类失败'
        };
      }
    } catch (error) {
      console.error('创建货物分类失败:', error);
      Message.error('创建货物分类失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  /**
   * 更新货物分类
   * @param {Number} id - 分类ID
   * @param {Object} data - 分类数据
   * @returns {Promise} 请求结果
   */
  async function updateCategory(id, data) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.updateCategory(id, data);
      
      if (response.code === 0) {
        Message.success('更新货物分类成功');
        return {
          success: true,
          data: response.data
        };
      } else {
        Message.error(response.message || '更新货物分类失败');
        return {
          success: false,
          message: response.message || '更新货物分类失败'
        };
      }
    } catch (error) {
      console.error('更新货物分类失败:', error);
      Message.error('更新货物分类失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  /**
   * 删除货物分类
   * @param {Number} id - 分类ID
   * @returns {Promise} 请求结果
   */
  async function deleteCategory(id) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.deleteCategory(id);
      
      if (response.code === 0) {
        Message.success('删除货物分类成功');
        return {
          success: true
        };
      } else {
        Message.error(response.message || '删除货物分类失败');
        return {
          success: false,
          message: response.message || '删除货物分类失败'
        };
      }
    } catch (error) {
      console.error('删除货物分类失败:', error);
      Message.error('删除货物分类失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  /**
   * 批量删除货物分类
   * @param {Array<Number>} ids - 分类ID数组
   * @returns {Promise} 请求结果
   */
  async function batchDeleteCategories(ids) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.batchDeleteCategories(ids);
      
      if (response.code === 0) {
        Message.success('批量删除货物分类成功');
        return {
          success: true
        };
      } else {
        Message.error(response.message || '批量删除货物分类失败');
        return {
          success: false,
          message: response.message || '批量删除货物分类失败'
        };
      }
    } catch (error) {
      console.error('批量删除货物分类失败:', error);
      Message.error('批量删除货物分类失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  /**
   * 更新分类排序
   * @param {Array} data - 分类排序数据
   * @returns {Promise} 请求结果
   */
  async function updateCategoryOrder(data) {
    loading.value = true;
    try {
      const response = await cargoCategoryApi.updateCategoryOrder(data);
      
      if (response.code === 0) {
        Message.success('更新分类排序成功');
        return {
          success: true
        };
      } else {
        Message.error(response.message || '更新分类排序失败');
        return {
          success: false,
          message: response.message || '更新分类排序失败'
        };
      }
    } catch (error) {
      console.error('更新分类排序失败:', error);
      Message.error('更新分类排序失败，请稍后重试');
      return {
        success: false,
        message: '网络错误，请稍后重试'
      };
    } finally {
      loading.value = false;
    }
  }

  return {
    loading,
    categories,
    categoryDetail,
    getCategoryList,
    getCategoryDetail,
    createCategory,
    updateCategory,
    deleteCategory,
    batchDeleteCategories,
    updateCategoryOrder
  };
}); 